﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AdventureWorksDataAccessTests.cs" company="Believe2014">
//   WhenYouBelieve2014@gmail.com
// </copyright>
// <summary>
//   The adventure works data access tests.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Configuration;
using System.Linq;
using System.Reflection;
using EntityInterfaceGenerator.Example.AdventureWorksDataAccess;
using JetBrains.Annotations;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTests_ActualDatabase
{
    /// <summary>
    ///     The adventure works data access tests.
    /// </summary>
    [TestClass]
    public class AdventureWorksDataAccessTests
    {
        /// <summary>
        ///     The smoke test.
        /// </summary>
        [TestMethod]
        public void SmokeTest()
        {
            using (AdventureWorksEntities context = getAdventureWorksEntities())
            {
                if (context.Database != null) context.Database.Log = Console.Write;

                // lazy and dont want to research why this isnt loading 
                // use reflection to iterate over each collection in the context and just watch for errors.
                Type t = context.GetType();
                if (t == null) return;
                PropertyInfo[] props = t.GetProperties();
                foreach (PropertyInfo p in props)
                {
                    if (p == null) continue;

                    bool isEnumerable = (from i in p.PropertyType.GetInterfaces()
                        where i == typeof (IEnumerable)
                        select i
                        ).Any();

                    if (!isEnumerable) continue;

                    try
                    {
                        var dataToPull = (IEnumerable) p.GetValue(context, null);
                        if (dataToPull == null) continue;

                        // ReSharper disable once UnusedVariable
                        object actual = (from object d in dataToPull select d).FirstOrDefault();
                    }
                    catch (Exception ex)
                    {
                        string error = ex.Message;
                        if (ex.InnerException != null)
                            error += ex.InnerException.Message;
                        Assert.Fail(error);
                    }
                }
            }
        }

        /// <summary>
        /// The get adventure works entities.
        /// </summary>
        /// <returns>
        /// The <see cref="AdventureWorksEntities"/>.
        /// </returns>
        /// <exception cref="ConfigurationErrorsException">
        /// </exception>
        [NotNull]
        private static AdventureWorksEntities getAdventureWorksEntities()
        {
            AdventureWorksEntities context;
            try
            {
                context = new AdventureWorksEntities();
            }
            catch (Exception error)
            {
                throw new ConfigurationErrorsException(
                    "Check connection string in app.config file. The AdventureWorksEntities connection string is required.", 
                    error);
            }

            return context;
        }
    }
}